jQuery(function($) {
	var doc = $(document);
	
	//错误信息
	function errorMessage(node, msg) {
		node.show().html(msg);
	}
	//清除错误信息
	function clearErrorMessage(node) {
		node.hide().html("");
	}
	
	//表单是否为空
	function empty(str) {
		if (str === "") {
			return true;
		}
		return false;
	}
	
	//验证链接
	/*function checkURI(url) {
		var s =  /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
		return s.test(url);
	}*/
	//请空错误信息
	function clearErrorMessage(node) {
		 node.html("").hide();
	}
	
	//验证所有信息
	var done = {
		proName: false,
		desc: false,
		//cUrl: false,
		//iUrl: false,
		//aUrl: false,
		memName: false,
		jobTitle: false
	};
	
	
	
	/***验证项目名称***/
	function checkProName(elm) {
		var val = elm.val();
		
		if (empty(val)) {
			done.proName = false;
			errorMessage($(".c-e-1"), "项目名称不能为空!");
			return;
		}
		if (val.length > 20) {
			done.proName = false;
			errorMessage($(".c-e-1"), "项目名称字数请保持在20个字符以内!");
			return;
		}
		
		done.proName = true;
	}
	
	doc.on("blur", ".commpany_name", function() {
			checkProName($(this));
	});
	
	doc.on("focus", ".commpany_name", function() {
		clearErrorMessage($(".c-e-1"));
	});
	/***end***/
	
	
	/***团队信息***/
	var team = {
		member: false,
		role: false,
		desc: false
		
	};
		//插入成员信息
	function innerMember(elm, name, role) {
		elm.before(
			'<div class="dl-box">' +
				"<span>" +
					'<button class="edit_btn"></button>' +
					'<button class="close_btn"></button>' +
				'</span>' +
				'<dl>' +
					'<dt><a href="#"><img src="images/userphoto_5.png"></a></dt>' +
					'<dd>' +
						'<p><strong>' + name + '</strong></p>' +
						'<p>' + role + '</p>' +
					'</dd>' +
				'</dl>' +
			'</div>'
		);
	}
		//添加您的团队成员
	function checkMember(input, obj, key,  err, msg1, msg2, len) {
		var val = input.val();
		
		if (empty(val)) {
			errorMessage(err, msg1);
			team[key] = false;
			return;
		}
		
		if (val.length > len) {
			errorMessage(err, msg2);
			team[key] = false;
			return;
		}
		team[key] = true;
	}
		//姓名
	doc.on("blur", ".member_name", function() {
		checkMember($(this),team, "member", $(".mem_err"),  "姓名不能为空！", "姓名长度上限为4个字符！", 5);
	});
	doc.on("focus", ".member_name", function() {
		 clearErrorMessage($(".mem_err"));
	});
	
		//角色
	doc.on("blur", ".role", function() {
		checkMember($(this), team, "role", $(".role_err"), "角色不能为空！", "角色长度上限为10个字符！", 11);
	});
	
	doc.on("focus", ".role", function() {
		 clearErrorMessage($(".role_err"));
	});
		//成员简介
	doc.on("blur", ".add_team_table .desc", function() {
		checkMember($(this), team, "desc", $(".desc_err"), "角色不能为空！", "角色长度上限为1000个字符！", 1001);
	});
	doc.on("focus", ".add_team_table .desc", function() {
		clearErrorMessage($(".desc_err"));
	});

		//添加团队成员
	doc.on("click", ".add_team_table .add", function() {
		checkMember($(".member_name"),team, "member", $(".mem_err"),  "姓名不能为空！", "姓名长度上限为4个字符！", 5);
		checkMember($(".role"), team, "role", $(".role_err"), "角色不能为空！", "角色长度上限为10个字符！", 11);
	
		if (team.member &&
			team.role) {
			innerMember($(".add-box"), $(".member_name").val(), $(".role").val());
		}
	});
	
		//删除团队成员
	var time = {
		timer: 0
	};
	
	function removeElement(elm) {
		clearInterval(time.timer);
		time.timer = setTimeout(function() {
			elm.remove();
		}, 500);
	}
	
	function removeTeamMember(btn) {
		var memberList = $(".team .dl-box"),
			parent = btn.parent().parent(),
			index = parent.index();

		memberList.eq(index).addClass("fadeout");
		removeElement(parent);
	}
	
	doc.on("click", ".team .close_btn", function() {
		removeTeamMember($(this));
	});
	/***团队信息 - end - ***/
	
	/***里程碑 - start - ***/
	function innerLandmark(elm, date, text) {console.log(date, text);
		elm.before(
			'<div class="dl-box">' +
				'<div>' +
					'<time>' + date + '</time>' +
					'<p>' +	text + '</p>' +
				'</div>'+
			'</div>'
		);
	}
	
	function checkLandmark(textarea) {
		var val = textarea.val();
		
		if (empty(val)) {
			errorMessage($(".landmark_err"), "里程碑内容不能为空！");
			return;
		}
		
		if (val.length > 51) {
			errorMessage($(".landmark_err"), "里程碑内容长度限制为50个字符！");
			return;
		}
	}
	
	doc.on("blur", ".add_landmark_table textarea", function() {
		checkLandmark($(this));
	});
	
	doc.on("focus", ".add_landmark_table textarea", function() {
		$(".landmark_err").html("").hide();
	});
	
	doc.on("click", ".add_landmark_table .add_btn", function() {
		var areaVal = $(".add_landmark_table textarea").val(),
			year    = $(".add_landmark_table .year").val(),
			month   = $(".add_landmark_table .month").val(),
			box     = $(".land-box .add-land");
			
		if (!empty(areaVal)) {
			innerLandmark(box, year + "/" + month , areaVal);
		}
		
	});

	$('#dateOnlyExample .date').datepicker({
		'format': 'yyyy-m-d',
		'autoclose': true
	});
	//提交代码
	
	
});